<?php

namespace App\Console\Commands\Command\Init;

use App\Console\Commands\Command\GenerateTrainStationMap;
use App\Console\Commands\Command\UpdateAreas;
use App\Console\Commands\Command\UpdateAreaTranslationBy;
use App\Console\Commands\Command\UpdateTrainStationTranslationBy;
use App\Trip\Services\Train\Station\StationMap;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;

class ApplicationInit
{
    /**
     * @throws GuzzleException
     * @throws BindingResolutionException
     */
    static function init()
    {
        if (app_is_init()) {
            return "系统已初始化\n";
        }

        $cache_time = config('trip.ttl.day') * 10;

        // JWT_SECRET init
        if (!config('jwt.secret')) {
            Artisan::call('jwt:secret');
            return "请先初始化 jwt, php artisan  jwt:secret \n";
        }
        if (!config('trip.aes.key')) {
            return "请先初始化 AES配置,php artisan aes:init\n";
        }
        if (!config('trip.aes.iv')) {
            return "请先初始化 AES_IV\n";
        }

        //  1 先采集地区
        $cache_key = "AppInit:UpdateAreas:fetchAll";
        if (!Cache::has($cache_key)) {
            UpdateAreas::fetchAll();
            Cache::put($cache_key, 1, $cache_time);
        }
        //  1.1 导入翻译
        $cache_key2 = "AppInit:UpdateAreaTranslationBy:byExcel";
        try {
            if (!Cache::has($cache_key2)) {
                UpdateAreaTranslationBy::byExcel();
                Cache::put($cache_key2, 1, $cache_time);
            }
        } catch (Exception $exception) {
            Cache::forget($cache_key2);
            return "地区翻译异常:".$exception->getMessage()."\n";
        }
        // 2 采集火车站
        // official
        $cache_key3 = "AppInit:addOrUpdateStations:Official";
        if (!Cache::has($cache_key3)) {
            Artisan::call('crawl:official_stations');
            Cache::put($cache_key3, 1, $cache_time);
        }
        // ctrip
        $cache_key4 = "AppInit:addOrUpdateStations:Ctrip";
        if (!Cache::has($cache_key4)) {
            Artisan::call('crawl:ctrip_stations');
            Cache::put($cache_key4, 1, $cache_time);
        }
        // 2.1 导入翻译
        $cache_key5 = "AppInit:UpdateTrainStationTranslationBy:byExcel";
        if (!Cache::has($cache_key5)) {
            UpdateTrainStationTranslationBy::byExcel();
            Cache::put($cache_key5, 1, $cache_time);
        }
//         2.2 匹配站点和城市
        $cache_key6 = "AppInit:GenerateTrainStationMap:matchStationCity";
        if (!Cache::has($cache_key6)) {
            $gm = new GenerateTrainStationMap();
            $gm->matchStationCity();
            Cache::put($cache_key6, 1, $cache_time);
        }
//         生成站点映射
        $cache_key7 = "AppInit:StationMap:write";
        if (!Cache::has($cache_key7)) {
            $station_map = new StationMap;
            $station_map->write();
            Cache::put($cache_key7, 1, $cache_time);
        }
        app_initialized();
        return "已完成";
    }


}